package com.rlovep.contact.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.rlovep.contact.dao.ContactDao; import com.rlovep.contact.dao.impl.ContactDaoImpl; import com.rlovep.contact.entity.Contact; /** * Servlet implementation class AddContact */ @WebServlet("/AddContact") public class AddContact extends HttpServlet { private static final long serialVersionUID = 1L; public AddContact() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); //接收参数 String name = request.getParameter("name"); String gender = request.getParameter("gender"); String age = request.getParameter("age"); String phone = request.getParameter("phone"); String email = request.getParameter("email"); String qq = request.getParameter("qq"); //封装成对象 Contact contact = new Contact(); contact.setName(name); contact.setGender(gender); contact.setAge(Integer.parseInt(age)); contact.setPhone(phone); contact.setEmail(email); contact.setQq(qq); //添加联系人 ContactDao contactDao=new ContactDaoImpl(); contactDao.addContact(contact); //3.跳转到查询页面 response.sendRedirect(request.getContextPath()+"/ListContact"); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }